package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/10/25.
 */

public class ReceiveGiftAdapter extends BaseAdapter {
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();
    public ReceiveGiftAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<EatShowUserInfo> list){
        if (mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (mList != null && mList.size()>0){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size()>0){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.receive_gift_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(mList.get(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(EatShowUserInfo info, ViewHolder holder) {
        if (info == null )return;
        String headUrl = info.getUserLogo();
        if (!ViewUtil.isNull(headUrl)){
            ViewUtil.Size size = ViewUtil.getHeadImageSize(mContext,50);
            String cache_key = ImageCache.getInstance(mContext).getCacheKey(headUrl,size);
            Bitmap cache_bitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(cache_key);
            if (cache_bitmap != null){
                holder.headImage.setImageBitmap(cache_bitmap);
            }else {
                holder.headImage.setTag(headUrl);
                ImageCache.getInstance(mContext).loadBitmapToView(holder.headImage, headUrl, size);
            }
        }else {
            holder.headImage.setImageResource(R.drawable.default_head_image);
        }

        String zhubo_nick = info.getNickName();
        try {
            zhubo_nick = URLDecoder.decode(zhubo_nick,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        holder.fansNick.setText(zhubo_nick);
        String content = mContext.getString(R.string.receive_total_jibi,info.getFlowerCount());
        holder.sendTotalJibi.setText(content);
    }

    protected class ViewHolder {
        private RoundImageView headImage;
        private TextView fansNick;
        private TextView sendTotalJibi;

        public ViewHolder(View view) {
            headImage = (RoundImageView) view.findViewById(R.id.head_image);
            fansNick = (TextView) view.findViewById(R.id.fans_nick);
            sendTotalJibi = (TextView) view.findViewById(R.id.send_total_jibi);
        }
    }
}
